package com.ccl.iot.dvr.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * SQLite工具类
 * <p>
 * 创建数据库，更新数据库。
 */
public class DBHelper extends SQLiteOpenHelper {
    private static final String TAG = "DBHelper";
    // 数据库版本，版本号从1开始
    private static final int VERSION = 1;
    // 数据库名字
    private static final String DBNAME = "dvr.db";

    private static DBHelper helper;

    public static final String CREATE_DVR_TABLE =
            //
            "CREATE TABLE IF NOT EXISTS [DVRData] ("
                    // 0行
                    + "[_id] integer primary key autoincrement,"
                    // 1
                    + "[IP] nvarchar(100),"
                    // 2
                    + "[port] integer, "
                    // 3
                    + "[user] nvarchar(100),"
                    // 4
                    + "[pwd] nvarchar(100),"
                    // 5
                    + "[channel] integer,"
                    // 6
                    + "[channelName] nvarchar(100),"
                    // 7
                    + "[channelCount] integer,"
                    // 8
                    // 是否在流畅模式 0：否 ; 1:是
                    + "[isSmooth] integer,"
                    // 9
                    // 是否wifi时高清模式 0：否 ; 1:是
                    + "[isHightDefintionInWifi] integer,"
                    // 10
                    + "[deviceID] integer,"
                    // 11
                    + "[deviceName] nvarchar(100),"
                    // 12
                    // 是否打开监控 0：不在监控 ; 1:在监控
                    + "[dvrState] integer,"
                    // 13 设备类型
                    + "[type] integer,"
                    // 14 监控品牌类型名字
                    + "[dvrName] integer,"
                    // 15 摄像头序号
                    + "[cameraNo] integer,"
                    // 16 萤石设备数据
                    + "[deviceInfo] nvarchar(1000),"
                    // 17 萤石账号名
                    + "[ysUser] nvarchar(100),"

                    + "[isOrientation] integer"

                    + ")";


    public DBHelper(Context context) {
        super(context, DBNAME, null, VERSION);
    }

    // 将自定义的数据库创建类单例。
    public static synchronized DBHelper getInstance(Context context) {
        if (helper == null) {
            helper = new DBHelper(context);// 数据库名称为create_db。
        }
        return helper;
    }

    // 创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_DVR_TABLE);

    }

    // 当版本号VERSION升级时，执行更新数据库
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

//		if (oldVersion == 1 && newVersion == 2) {
//			// 测试-------------
//			// 从版本1到版本2时，增加了一个字段 desc
//			String sql = "alter table DVRData add [isOrientation] integer";
//			db.execSQL(sql);
//		}

    }

}
